package test;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.SimpleTimeZone;

public class StudentSystem {
    public static void startStuendtsystem() {
        ArrayList<Student> list = new ArrayList<Student>();
        while (true) {
            System.out.println("---------欢迎来到陈昊的学生管理系统--------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出");
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch (choose) {
                case "1" -> addStudent(list);
                case "2" -> dealStudent(list);
                case "3" -> xgStudent(list);
                case "4" -> cxStudent(list);
                case "5" -> {
                    System.out.println("退出");
                    System.exit(0);

                }
                default -> System.out.println("没有这个选项");
            }
        }
    }

    private static void cxStudent(ArrayList<Student> list) {
        int len = list.size();
        if (len == 0) {
            System.out.println("当前无学生信息,请添加后查询");
            return;
        }
        //走到这里有了学生信息
        //打印标题信息
        System.out.println("id\t姓名\t年龄\t地址");
        for (int i = 0; i < len; i++) {
            Student stu = list.get(i);
            System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getDz());
        }
    }

    private static void xgStudent(ArrayList<Student> list) {

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的id");
        String id = sc.next();
        int index = contains(list, id);
        if (index == -1) {
            System.out.println("id不存在");
            return;
        }
        Student stu = list.get(index);
        System.out.println("请输入新的姓名");
        String newname = sc.next();
        stu.setId(newname);
        System.out.println("请输入新的年龄");
        int newage = sc.nextInt();
        stu.setAge(newage);
        System.out.println("请输入新的地址");
        String newdz = sc.next();
        stu.setDz(newdz);


    }

    private static void dealStudent(ArrayList<Student> list) {
        System.out.println("请输入要删除id");
        Scanner sc = new Scanner(System.in);
        String id = sc.next();
        int index = contains(list, id);
        if (index == -1) {
            System.out.println("id不存在");
            return;
        } else {
            list.remove(index);
            System.out.println("删除成功");
        }
    }

    private static void addStudent(ArrayList<Student> list) {
        //id唯一
        Student stu = new Student();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入id");
            String id = sc.next();
            int index = contains(list, id);
            if (index == -1) {
                stu.setId(id);
                break;
            } else {
                System.out.println("id存在,请重新输入");
            }
        }

        System.out.println("请输入学生姓名");

        String name = sc.next();
        stu.setName(name);
        System.out.println("请输入学生的年龄");
        int age = sc.nextInt();
        stu.setAge(age);
        System.out.println("请输入学生的地址");
        String dz = sc.next();
        stu.setDz(dz);
        list.add(stu);
        System.out.println("添加成功");

    }

    private static int contains(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu.getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }
}
